package leetcode.code1239;

import java.util.List;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int maxLength(List<String> arr) {
		int len = arr.size();
		int[] mask = new int[len];
		for (int i = 0; i < mask.length; i++) {
			mask[i] = this.mask(arr.get(i));
		}
		int ans = 0;
		for (int i = 1; i < (1 << len); i++) {
			int cur = 0;
			boolean ok = true;
			for (int j = 0; j < len; j++) {
				if ((i & (1 << j)) != 0) {
					if (mask[j] == -1) {
						ok = false;
						break;
					}
					if ((cur & mask[j]) == 0) {
						cur |= mask[j];
					} else {
						ok = false;
						break;
					}
				}
			}
			if (ok)
				ans = Math.max(ans, Integer.bitCount(cur));
		}
		return ans;
	}

	private int mask(String string) {
		int ans = 0;
		for (int i = 0; i < string.length(); i++) {
			int pos = 1 << (string.charAt(i) - 'a');
			if ((ans & pos) == 0) {
				ans |= pos;
			} else {
				return -1;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(4, this.maxLength(H.str2liststr("[\"un\",\"iq\",\"ue\"]")));
		H.compare(6, this.maxLength(H.str2liststr("[\"cha\",\"r\",\"act\",\"ers\"]")));
		H.compare(26, this.maxLength(H.str2liststr("[\"abcdefghijklmnopqrstuvwxyz\"]")));
		H.compare(5, this.maxLength(H.str2liststr("[\"chaa\",\"r\",\"ct\",\"ers\"]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
